package card_discount

import (
	sqlc "bp/db/sqlc"
	"bp/internal/render"
	cardDiscount "bp/internal/service/entity/card_discount"
	"bp/internal/util"
	cardDiscountGrpc "bp/rpc/gen/webapi/card_discount"
	"context"
)

// CreateCardDiscount 新建卡折扣
func (s *CardDiscountWebApiImp) CreateCardDiscount(ctx context.Context, req *cardDiscountGrpc.CreateCardDiscountReq) (res *cardDiscountGrpc.CreateCardDiscountRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("card_discount_imp/cmd.CreateCardDiscount=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &cardDiscountGrpc.CreateCardDiscountRes{}

	var cardDiscountPtr *sqlc.CardDiscount
	var conv CardDiscountPtrEntityToPbFunc
	err = s.dao.NewTxRunner().SqlcExecTx(ctx, func(q *sqlc.Queries) (err error) {
		createCardDiscountParamPtr, itemCategoryConfPtrs, itemConfPtrs, err := s.VldCreateCardDiscountReq(ctx, q, req)
		if err != nil {
			return
		}
		cardDiscountPtr, err = s.entityHub.CardDiscountService.CreateCardDiscount(ctx, q, createCardDiscountParamPtr)
		if err != nil {
			return
		}
		err = s.entityHub.CardDiscountService.SetCardDiscountItemCategories(ctx, q, &cardDiscount.SetCardDiscountItemCategoriesParam{
			CardDiscountPtr:            cardDiscountPtr,
			ItemCategoryConfPtrs: itemCategoryConfPtrs,
		})
		if err != nil {
			return
		}
		err = s.entityHub.CardDiscountService.SetCardDiscountItems(ctx, q, &cardDiscount.SetCardDiscountItemsParam{
			CardDiscountPtr:    cardDiscountPtr,
			ItemConfPtrs: itemConfPtrs,
		})
		if err != nil {
			return
		}
		_, err = s.entityHub.CardDiscountService.EnsureCardDiscountStat(ctx, q, false, &cardDiscount.EnsureCardDiscountStatParam{
			CardDiscountPtr: cardDiscountPtr,
		})
		if err != nil {
			return
		}
		conv = s.ConvCardDiscountPtrEntityToPb(itemCategoryConfPtrs, itemConfPtrs)
		slog.Infof("新建卡折扣 ok")
		return
	})
	if err != nil {
		return
	}

	res.CardDiscount = conv(cardDiscountPtr)

	return
}

// UpdateCardDiscount 更新卡折扣
func (s *CardDiscountWebApiImp) UpdateCardDiscount(ctx context.Context, req *cardDiscountGrpc.UpdateCardDiscountReq) (res *cardDiscountGrpc.UpdateCardDiscountRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("card_discount_imp/cmd.UpdateCardDiscount=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &cardDiscountGrpc.UpdateCardDiscountRes{}

	var cardDiscountPtr *sqlc.CardDiscount
	var conv CardDiscountPtrEntityToPbFunc
	err = s.dao.NewTxRunner().SqlcExecTx(ctx, func(q *sqlc.Queries) (err error) {
		updateCardDiscountParamPtr, itemCategoryConfPtrs, itemConfPtrs, err := s.VldUpdateCardDiscountReq(ctx, q, req)
		if err != nil {
			return
		}
		cardDiscountPtr, err = s.entityHub.CardDiscountService.UpdateCardDiscount(ctx, q, updateCardDiscountParamPtr)
		if err != nil {
			return
		}
		err = s.entityHub.CardDiscountService.SetCardDiscountItemCategories(ctx, q, &cardDiscount.SetCardDiscountItemCategoriesParam{
			CardDiscountPtr:            cardDiscountPtr,
			ItemCategoryConfPtrs: itemCategoryConfPtrs,
		})
		if err != nil {
			return
		}
		err = s.entityHub.CardDiscountService.SetCardDiscountItems(ctx, q, &cardDiscount.SetCardDiscountItemsParam{
			CardDiscountPtr:    cardDiscountPtr,
			ItemConfPtrs: itemConfPtrs,
		})
		if err != nil {
			return
		}
		conv = s.ConvCardDiscountPtrEntityToPb(itemCategoryConfPtrs, itemConfPtrs)
		slog.Infof("更新卡折扣 ok")
		return
	})
	if err != nil {
		return
	}

	res.CardDiscount = conv(cardDiscountPtr)
	return
}

// MarkCardDiscountPaused 标记卡折扣停用
func (s *CardDiscountWebApiImp) MarkCardDiscountPaused(ctx context.Context, req *cardDiscountGrpc.MarkCardDiscountPausedReq) (res *cardDiscountGrpc.MarkCardDiscountPausedRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("card_discount_imp/cmd.MarkCardDiscountPaused=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &cardDiscountGrpc.MarkCardDiscountPausedRes{}

	var cardDiscountPtr *sqlc.CardDiscount
	err = s.dao.NewTxRunner().SqlcExecTx(ctx, func(q *sqlc.Queries) (err error) {
		markCardDiscountParamPtr, err := s.VldMarkCardDiscountPausedReq(ctx, q, req)
		if err != nil {
			return
		}
		cardDiscountPtr, err = s.entityHub.CardDiscountService.MarkCardDiscountPaused(ctx, q, markCardDiscountParamPtr)
		if err != nil {
			return
		}
		slog.Infof("标记卡折扣停用 ok")
		return
	})
	if err != nil {
		return
	}

	res.IsPaused = cardDiscountPtr.IsPaused
	return
}

// UnMarkCardDiscountPaused 取消标记卡折扣停用
func (s *CardDiscountWebApiImp) UnMarkCardDiscountPaused(ctx context.Context, req *cardDiscountGrpc.UnMarkCardDiscountPausedReq) (res *cardDiscountGrpc.UnMarkCardDiscountPausedRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("card_discount_imp/cmd.UnMarkCardDiscountPaused=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &cardDiscountGrpc.UnMarkCardDiscountPausedRes{}

	var cardDiscountPtr *sqlc.CardDiscount
	err = s.dao.NewTxRunner().SqlcExecTx(ctx, func(q *sqlc.Queries) (err error) {
		unMarkCardDiscountParamPtr, err := s.VldUnMarkCardDiscountPausedReq(ctx, q, req)
		if err != nil {
			return
		}
		cardDiscountPtr, err = s.entityHub.CardDiscountService.UnMarkCardDiscountPaused(ctx, q, unMarkCardDiscountParamPtr)
		if err != nil {
			return
		}
		slog.Infof("取消标记卡折扣停用 ok")
		return
	})
	if err != nil {
		return
	}

	res.IsPaused = cardDiscountPtr.IsPaused
	return
}
